import require from '@/utils/request'
import { SAVE_USER, SAVE_PROFILE, UPDATE_PROFILE, LOGIN_LOGOUT } from '@/store/action_types/profile'
import { removeTokenInfo } from '@/utils/storage'

export const getUser = () => {
    return async dispatch => {
        const res = await require({
            url: '/user',
            method: 'get'
        })

        dispatch({
            type: SAVE_USER,
            payload: res.data
        })
    }
}

export const getProfile = () => {
    return async dispatch => {
        const res = await require({
            url: '/user/profile',
            method: 'get'

        })

        dispatch({
            type:SAVE_PROFILE,
            payload: res.data
        })
    }
}

export const updateProfile = (data) => {
    return async dispatch => {
        const res = await require({
            url: '/user/profile',
            method: 'patch',
            data
        })

        dispatch({
            type:UPDATE_PROFILE,
            payload: data
        })
    }
}

export const updatePhoto = (formData) => {
    return async dispatch => {
        const res = await require.patch('/user/photo', formData, {
			headers: {
				'Content-Type': 'multipart/form-data',
			},
		})
        
        dispatch(getProfile())
    }
}

